<template>
    <div style="width:100%;height:100%;display: inline-block;position: relative;">
        <!-- @error="onError($event)" -->
        <video-player
            ref="videoPlayer"
            class="video-player vjs-custom-skin Live"
            :playsinline="true"
            :options="playerOptions"
            @waiting="onPlayerWaiting($event)" 
            @error="onError($event)"
            @dblclick="fullScreen"
            :key="tv">
        </video-player>
        <div v-if="istvNum" class="tvNum">
            <span  style="font-size: 30px;font-weight: 600;">{{ts}}</span>
        </div>
    </div>
</template>

<script>
import json from '@/assets/json/sources.json'
export default {
    data () {
        return {
            tv:-1,
            ts:this.tv+1,
            tvList:json,
            istvNum:false,
            isError:false,
            playerOptions: {
                autoplay: true, // 如果true,浏览器准备好时开始回放。
                muted: false, // 默认情况下将会消除任何音频。
                preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
                language: 'zh-CN',
                aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
                fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
                techOrder: ['html5'],
                live: true,
                html5: { 
                    hls: { withCredentials: false } 
                },
                sources: json,
                hls: true,
                notSupportedMessage: '　', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
                controlBar: {
                    timeDivider: false,
                    durationDisplay: false,
                    remainingTimeDisplay: false,
                    fullscreenToggle: false // 全屏按钮
                },
            },
            isTime:null,
            apple:0
        }
    },
    computed: {
        player() {
            this.$refs.videoPlayer.player.error_=this.onError
            return this.$refs.videoPlayer.player
        }
    },
    created(){
        this.$http.get('https://gitee.com/YeXingKong/MinTV/raw/master/TVsources/sources.json').then((res)=>{
            let tv=localStorage.getItem('TV')
            if(!tv){
                tv=0
            }
            this.tv=Number(tv)
            this.tvList=res.data
            this.changeVideo(this.tv)
        }).catch(()=>{
            this.tvList=json
            this.changeVideo(this.tv)
        })
        
        
    },
    watch:{
        
        istvNum:function(val){
            
            if(this.isTime){
                window.clearTimeout(this.isTime)
            }
            if(val){
                this.isTime=setTimeout(()=>{
                    this.istvNum=false
                },3000)
            }
        }
    },
    methods:{
        fullScreen:function(){
            this.player.requestFullscreen();
            this.player.enterFullWindow();
            this.player.isFullscreen(true);
        },
        onPlayerWaiting:function(e){
            console.log(e)
            this.setVolume(this.value)
        },
        onError:function(){
            console.log('error')
            this.$emit('error')
        },
        setVolume:function(val){
            this.player.volume(val)
        },
        changeNumVideo:function(num){
            this.tv+=num
            this.changeVideo(this.tv)
        },
        changeVideo:function(tv){
            this.istvNum=true
            
            if(this.tv<0){
                this.tv=this.tvList.length-1
            }
            if(this.tv+1>this.tvList.length){
                this.tv=0
            }
            localStorage.setItem('TV',this.tv)
            this.ts=this.tv+1
            console.log(this.ts)
            this.playerOptions.sources[0]=this.tvList[this.tv]
            console.log(this.tvList[this.tv])
            
            this.$emit('changeTVNum',this.ts,this.playerOptions.sources[0].title)
        }
    }
}
</script>

<style scoped>
.Live>>>.video-js .vjs-tech {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
.Live>>>.video-js{
    background-color:rgba(0,0,0,0)
} 
.Live>>>.vjs-has-started.vjs-user-inactive.vjs-playing .vjs-control-bar {
    opacity: 0;
    display: none;
}
.Live>>>.vjs-loading-spinner{
    display: none;
    opacity: 0;
}
.Live >>> .vjs-big-play-button{
    display: none;
    opacity: 0;
}
.Live >>> .video-js .vjs-modal-dialog {
    opacity: 0;
}
.Live >>>.vjs-has-started .vjs-control-bar{
    display: none;
    opacity: 0;
}
.tvNum{
    position: absolute;
    top: 25px;
    right: 20px;
    color: #01f3f5;
    width: 70px;
    text-align: right;
}
</style>
